package com.my.user.service.impl;

import com.my.user.dao.IUserDao;
import com.my.user.dao.impl.UserDaoImpl;
import com.my.entity.User;
import com.my.user.service.IUserService;

import java.util.ArrayList;
import java.util.List;

//业务逻辑层：逻辑型的增删改查
public class UserServiceImpl implements IUserService {
    IUserDao dao = new UserDaoImpl();
    public boolean addUser(User user){
        if(!dao.isExist(user.getName())){
            return dao.addUser(user);
        }else{
            System.out.println("此用户已存在！");
            return false;
        }
    }
    public boolean deleteUser(User user){
        if(dao.isExist(user.getName())){
            return dao.deleteUserByName(user.getName());
        }else{
            System.out.println("此用户不存在！");
            return false;
        }
    }
    public boolean updatePwd(User user,String pwd){
        if(dao.pwdIsRight(user)){
            User user2 = new User(user.getName(), pwd);
            return dao.updateUserByName(user2);
        }else{
            System.out.println("用户名或密码不正确！");
            return false;
        }
    }

    public List<User> queryAllUsers(){
        List<User> users = new ArrayList<User>();
        users = dao.queryAllUsers();
        return users;
    }

    public User queryOneUser(String name){
        User user = dao.queryUserByName(name);
        return user;
    }

    @Override
    public int getTotalCount() {
        return dao.getTotalCount();
    }

    @Override
    public List<User> queryUserByPage(int currentPage, int pageSize) {
        return dao.queryUserByPage(currentPage,pageSize);
    }
}
